﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RPSImporter.Models;
using RPSImporter.Services;

namespace RPSImporter.Controllers
{
   public class UserController : BaseController
   {
      //
      // GET: /User/

      public ViewResult Index()
      {
         return View(DatabaseSvc.Instance.Settings.Users.ToList());
      }

      //
      // GET: /User/List

      public ActionResult List()
      {
         return PartialView(DatabaseSvc.Instance.Settings.Users.ToList());
      }

      //
      // GET: /User/Details/5

      public ViewResult Details(long id)
      {
         User user = DatabaseSvc.Instance.Settings.Users.Find(id);
         return View(user);
      }

      //
      // GET: /User/Create

      public ActionResult Create()
      {
         return View();
      }

      //
      // POST: /User/Create

      [HttpPost]
      public ActionResult Create(User user)
      {
         if (ModelState.IsValid)
         {
            DatabaseSvc.Instance.Settings.Users.Add(user);
            DatabaseSvc.Instance.Settings.SaveChanges();
            return RedirectToAction("Index");

         }

         return View(user);
      }

      //
      // GET: /User/Edit/5

      public ActionResult Edit(long id)
      {
         User user = DatabaseSvc.Instance.Settings.Users.Find(id);
         return View(user);
      }

      //
      // POST: /User/Edit/5

      [HttpPost]
      public ActionResult Edit(User user)
      {
         if (ModelState.IsValid)
         {
            DatabaseSvc.Instance.Settings.Entry(user).State = EntityState.Modified;
            DatabaseSvc.Instance.Settings.SaveChanges();
            return RedirectToAction("Index");
         }
         return View(user);
      }

      //
      // GET: /User/Delete/5

      public ActionResult Delete(long id)
      {
         User user = DatabaseSvc.Instance.Settings.Users.Find(id);
         return View(user);
      }

      //
      // POST: /User/Delete/5

      [HttpPost, ActionName("Delete")]
      public ActionResult DeleteConfirmed(long id)
      {
         User user = DatabaseSvc.Instance.Settings.Users.Find(id);
         DatabaseSvc.Instance.Settings.Users.Remove(user);
         DatabaseSvc.Instance.Settings.SaveChanges();
         return RedirectToAction("Index");
      }
   }
}